package cst.neu.neufooddemo.model;

import cst.neu.neufooddemo.common.Constans;
import cst.neu.neufooddemo.listener.RetrofitListener;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by liningning on 2017/10/21.
 */

public class RetrofitBaseModel {
    protected Retrofit retrofit;
    protected int flag;
    public RetrofitBaseModel()
    {
        retrofit=new Retrofit.Builder()
                .baseUrl(Constans.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public <T> void bindCallback(Call<T> call, final RetrofitListener<T> listener) {
        Callback<T> callback=new Callback<T>() {
            @Override
            public void onResponse(Call<T> call, Response<T> response) {
                listener.onSuccess(response.body(),flag);
            }

            @Override
            public void onFailure(Call<T> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);//绑定
    }

}

